package com.demo.outputformater;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

import static com.sun.jmx.snmp.ThreadContext.contains;

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private  FSDataOutputStream atguiguLog;
    private  FSDataOutputStream otherLog;

    public LogRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fileSystem = FileSystem.get(job.getConfiguration());
            atguiguLog = fileSystem.create(new Path("D:\\Code\\JavaCode\\MapReduceDemo\\test_data\\out\\outputformat\\atguigu.log"));
            otherLog = fileSystem.create(new Path("D:\\Code\\JavaCode\\MapReduceDemo\\test_data\\out\\outputformat\\other.log"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
        String s = text.toString();
        if(s.contains("atguigu")){
            atguiguLog.writeBytes(s+"\n");
        }else {
            otherLog.writeBytes(s+"\n");
        }

    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {

        IOUtils.closeStream(atguiguLog);
        IOUtils.closeStream(otherLog);
    }
}
